package pl.ps.tinydic;

import fr.castorflex.android.smoothprogressbar.SmoothProgressBar;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class Search extends Activity {
	private static final String WS_URL_PREFIX = "http://tinydefin.appspot.com/dictionary?w=";
	EditText et;
	WebView wv;
	SmoothProgressBar bar;
	ImageButton imageButton;
	Activity activity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_PROGRESS);
		this.setProgressBarVisibility(true);
		
		activity = this;
		
		setContentView(R.layout.activity_search);
		bar = (SmoothProgressBar) findViewById(R.id.pbc);
		et = (EditText) findViewById(R.id.searchet);
		imageButton = (ImageButton) findViewById(R.id.searchbt);
		et.setAlpha(0.9f);
		wv = (WebView) findViewById(R.id.resultWV);
		wv.setAlpha(0.9f);
		wv.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				if(newProgress==100) {
					bar.setVisibility(View.INVISIBLE);
					if(View.INVISIBLE==wv.getVisibility()) {
		        		// hide virtual keyboard
		                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		                imm.hideSoftInputFromWindow(et.getWindowToken(), 
		                                          InputMethodManager.RESULT_UNCHANGED_SHOWN);
		                
						wv.setVisibility(View.VISIBLE);
					}
				}
			}
		});
		et.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(KeyEvent.KEYCODE_CALL == actionId || KeyEvent.KEYCODE_ENTER == actionId
						|| KeyEvent.KEYCODE_ENDCALL == actionId) {
					String word = et.getText().toString();
					searchWord(word);
				}
				return false;
			}
		});
		imageButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String word = et.getText().toString();
				searchWord(word);
			}
		});
		
		// Get intent, action and MIME type
	    Intent intent = getIntent();
	    handleWordIntent(intent);
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		handleWordIntent(intent);
	}
	
	private void handleWordIntent(Intent intent) {
		String action = intent.getAction();
	    String type = intent.getType();
	    if (Intent.ACTION_SEND.equals(action) && type != null) {
	        if ("text/plain".equals(type)) {
	        	String word = intent.getStringExtra(Intent.EXTRA_TEXT);
	        	if(word!=null) {
	        		et.setText(word);
	        		searchWord(word); // Handle text being sent
	        	}
	        }
	    }
	}
	
	private void searchWord(String word) {
		bar.setVisibility(View.VISIBLE);
		wv.loadUrl(WS_URL_PREFIX+word);
	}

}
